package com.nand.module.function.adapter;

import android.content.Context;
import android.text.Html;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CompoundButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.appcompat.widget.AppCompatCheckBox;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.request.RequestOptions;
import com.nand.module.function.R;
import com.nand.module.function.bean.IqcListBean;
import com.nand.module.function.utils.ToolUtils;
import com.nand.module.function.view.AutofitTextView;

import java.util.ArrayList;
import java.util.List;

/**
 * created by 86185
 * on 2019/12/30
 */
public class IqcLldAdapter extends RecyclerView.Adapter<IqcLldAdapter.ViewHolder> implements View.OnClickListener {
    private Context mContext;
    private int type;
    private List<IqcListBean.DataListBean> mList = new ArrayList<>();

    private int code = 0;

    private IqcLldAdapter.OnItemClickListener mOnItemClickListener = null;
    private String replace;
    private RequestOptions error;

    public IqcLldAdapter(Context mContext, int type) {
        this.mContext = mContext;
        this.type = type;
    }

    public void setData(List<IqcListBean.DataListBean> data) {
        this.mList = data;
        notifyDataSetChanged();
    }

    public void setShowCBox(int code) {
        this.code = code;
        notifyDataSetChanged();
    }

    public List<IqcListBean.DataListBean> getCheckItemsBean() {
        return mList;
    }


    @Override
    public IqcLldAdapter.ViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
        View layout = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.item_iqc_lid, viewGroup, false);
        layout.setOnClickListener(this);
        return new IqcLldAdapter.ViewHolder(layout);
    }

    @Override
    public void onBindViewHolder(IqcLldAdapter.ViewHolder viewHolder, final int position) {
        viewHolder.itemView.setTag(position);

        final IqcListBean.DataListBean bean = mList.get(position);

        switch (type) {
            case 0:  //来料列表

                viewHolder.mTextview1.setText(Html.fromHtml("单据编号:&#8194;&#12288;<font color='#696969'>"+bean.getSendCheckID()+"</font>"));
                int state = bean.getState();
                if (0 == state) {
                    if (0 == code){
                        viewHolder.mCheckbox.setVisibility(View.GONE);
                        viewHolder.mCheckbox.setChecked(false);
                    }else {
                        viewHolder.mCheckbox.setVisibility(View.VISIBLE);
                    }
                    viewHolder.mTextview10.setText("保存");
                    viewHolder.mTextview10.setBackgroundResource(R.drawable.layout_color1);

                } else {
                    viewHolder.mTextview10.setText("已生效");
                    viewHolder.mCheckbox.setVisibility(View.GONE);
                    viewHolder.mTextview10.setBackgroundResource(R.drawable.layout_color4);
                }

                viewHolder.mTextview11.setText(Html.fromHtml("库&#12288;&#12288;房:&#8194;&#12288;<font color='#696969'>"+bean.getStoreName()+"</font>"));

                viewHolder.mTextview2.setText( bean.getBatchID());
                viewHolder.mTextview3.setText(Html.fromHtml("物料编码:&#8194;&#12288;<font color='#696969'>"+bean.getPartID()+"</font>"));
                viewHolder.mTextview4.setText(Html.fromHtml("物料名称:&#8194;&#12288;<font color='#696969'>"+bean.getPartName()+"</font>"));
                viewHolder.mTextview5.setText(Html.fromHtml("数&#12288;&#12288;量:&#8194;&#12288;<font color='#696969'>"+bean.getNumber()+"</font>"));
                String priority = bean.getPriority();
                //if ("3".equals(priority)) {
                    viewHolder.mTextview6.setText(Html.fromHtml("优&#8194;先&#8194;级:&#8194;&#12288;<font color='#696969'>"+priority+"</font>"));
               /* } else if ("2".equals(priority)) {
                    viewHolder.mTextview6.setText("优  先  级：    中");
                } else {
                    viewHolder.mTextview6.setText("优  先  级：    低");
                }*/

                viewHolder.mTextview7.setText(Html.fromHtml("供&#8194;应&#8194;商:&#8194;&#12288;<font color='#696969'>"+bean.getSupplierName()+"</font>"));

                viewHolder.mTextview8.setText(Html.fromHtml("创建时间:&#8194;&#12288;<font color='#696969'>"+bean.getCreatedTime()+"</font>"));

                String upName = bean.getUpName();
                if ("".equals(upName) || upName == null){
                    viewHolder.mTextview9.setVisibility(View.GONE);
                }else {
                    viewHolder.mTextview9.setText(Html.fromHtml("更新时间:&#8194;&#12288;<font color='#696969'>"+bean.getUpdatedTime() +"/"+upName+"</font>"));
                }
                //读取实体内存储的选中状态
                viewHolder.mCheckbox.setChecked(bean.getSelect());
                viewHolder.mCheckbox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                    @Override
                    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                        bean.setSelect(isChecked);
                    }
                });

                break;
            case 1: //检验单列表
                viewHolder.mTextview12.setVisibility(View.VISIBLE);
                viewHolder.mTextview1.setText(Html.fromHtml("单据编号:&#8194;&#12288;<font color='#696969'>"+bean.getCheckId()+"</font>"));
                int state1 = bean.getState();
                if (0 == state1) {
                    viewHolder.mTextview10.setText("未检验");
                    viewHolder.mTextview10.setBackgroundResource(R.drawable.layout_color3);
                } else {
                    viewHolder.mTextview10.setText("已检验");
                    viewHolder.mTextview10.setBackgroundResource(R.drawable.layout_color4);
                }
                viewHolder.mTextview2.setText(bean.getBatchID());
                viewHolder.mTextview3.setText(Html.fromHtml("物料编码:&#8194;&#12288;<font color='#696969'>"+bean.getPartCode()+"</font>"));
                viewHolder.mTextview4.setText(Html.fromHtml("物料名称:&#8194;&#12288;<font color='#696969'>"+bean.getPartName()+"</font>"));
                viewHolder.mTextview5.setText(Html.fromHtml("数&#12288;&#12288;量:&#8194;&#12288;<font color='#696969'>"+bean.getTotalNumber()+"</font>"));
                String priority2 = bean.getPriority();
                viewHolder.mTextview11.setText(Html.fromHtml("优&#8194;先&#8194;级:&#8194;&#12288;<font color='#696969'>"+priority2+"</font>"));
               /* if ("0".equals(priority2)) {
                    viewHolder.mTextview6.setText("优  先  级：    高");
                } else if ("1".equals(priority2)) {
                    viewHolder.mTextview6.setText("优  先  级：    中");
                } else {
                    viewHolder.mTextview6.setText("优  先  级：    低");
                }*/
                viewHolder.mTextview7.setText(Html.fromHtml("库&#12288;&#12288;房:&#8194;&#12288;<font color='#696969'>"+bean.getStoreName()+"</font>"));
                viewHolder.mTextview8.setText(Html.fromHtml("供&#8194;应&#8194;商:&#8194;&#12288;<font color='#696969'>"+bean.getSupplierName()+"</font>"));
                viewHolder.mTextview12.setText(Html.fromHtml("创建时间:&#8194;&#12288;<font color='#696969'>"+bean.getCreatedTime()+"/"+bean.getUserName()+"</font>"));
                String iqcNCheck = bean.getIqcNCheck();
                if ("0".equals(iqcNCheck)){
                    viewHolder.mTextview6.setText(Html.fromHtml("是否免检:&#8194;&#12288;<font color='#696969'>免检</font>"));
                }else if ("1".equals(iqcNCheck)){
                    viewHolder.mTextview6.setText(Html.fromHtml("是否免检:&#8194;&#12288;<font color='#696969'>不免检</font>"));
                }


                String upName2 = bean.getUpName();
                if ("".equals(upName2) || upName2 == null){
                    viewHolder.mTextview9.setVisibility(View.GONE);
                }else {
                    viewHolder.mTextview9.setText(Html.fromHtml("更新时间:&#8194;&#12288;<font color='#696969'>"+bean.getUpdatedTime()+"/"+upName2+"</font>"));
                }


                viewHolder.mCheckbox.setVisibility(View.GONE);
                break;
            case 2:
                viewHolder.mTextview1.setText(Html.fromHtml("单据编号:&#8194;&#12288;<font color='#696969'>"+bean.getIqcNGDataID()+"</font>"));
                int state2 = bean.getState();
                viewHolder.mTextview10.setBackground(null);
                if (0 == state2) {
                    viewHolder.mTextview10.setText("待处理");
                    viewHolder.mTextview10.setTextColor(mContext.getResources().getColor(R.color.color_FF3B5E));
                } else if (1== state2){
                    viewHolder.mTextview10.setText("待审核");
                    //viewHolder.mTextview10.setBackgroundResource(R.drawable.layout_color2);
                    viewHolder.mTextview10.setTextColor(mContext.getResources().getColor(R.color.color_F6BD32));
                }else {
                    viewHolder.mTextview10.setText("生效");
                   // viewHolder.mTextview10.setBackgroundResource(R.drawable.layout_color1);
                    viewHolder.mTextview10.setTextColor(mContext.getResources().getColor(R.color.color_38A677));
                }
                viewHolder.mTextview11.setVisibility(View.GONE);
                viewHolder.mTextview2.setText(bean.getIqcNGBatchID());
                viewHolder.mTextview3.setText(Html.fromHtml("物料编码:&#8194;&#12288;<font color='#696969'>"+bean.getPartID()+"</font>"));
                viewHolder.mTextview4.setText(Html.fromHtml("物料名称:&#8194;&#12288;<font color='#696969'>"+bean.getPartName()+"</font>"));
                viewHolder.mTextview5.setText(Html.fromHtml("不合格数量:&#12288;<font color='#696969'>"+bean.getNgNumber()+"</font>"));
                int type = bean.getType();
                switch (type) {
                    case 1:
                        viewHolder.mTextview6.setText(Html.fromHtml("物料类别:&#8194;&#12288;<font color='#696969'>成品</font>"));
                        break;
                    case 2:
                        viewHolder.mTextview6.setText(Html.fromHtml("物料类别:&#8194;&#12288;<font color='#696969'>半成品</font>"));
                        break;
                    default:
                        viewHolder.mTextview6.setText(Html.fromHtml("物料类别:&#8194;&#12288;<font color='#696969'>原材料</font>"));
                        break;
                }

                viewHolder.mTextview7.setText(Html.fromHtml("供&#8194;应&#8194;商:&#8194;&#12288;<font color='#696969'>"+bean.getSupplierName()+"</font>"));
                viewHolder.mCheckbox.setVisibility(View.GONE);
                viewHolder.mTextview8.setText(Html.fromHtml("创建时间:&#8194;&#12288;<font color='#696969'>"+bean.getCreatedTime()+"</font>"));
                viewHolder.mTextview9.setVisibility(View.GONE);
                break;
            default:
                break;
        }

    }

    @Override
    public int getItemCount() {
        if (mList != null)
            return mList.size();
        return 0;
    }

    class ViewHolder extends RecyclerView.ViewHolder {
        AutofitTextView mTextview2;
        TextView mTextview1, mTextview3, mTextview4, mTextview5, mTextview6, mTextview7, mTextview8,mTextview9,mTextview10,mTextview11,mTextview12;
        ImageView mIvIcon;
        AppCompatCheckBox mCheckbox;

        public ViewHolder(View itemView) {
            super(itemView);
            mTextview1 = itemView.findViewById(R.id.textview1);
            mTextview2 = itemView.findViewById(R.id.textview2);
            mTextview3 = itemView.findViewById(R.id.textview3);
            mTextview4 = itemView.findViewById(R.id.textview4);
            mTextview5 = itemView.findViewById(R.id.textview5);
            mTextview6 = itemView.findViewById(R.id.textview6);
            mTextview7 = itemView.findViewById(R.id.textview7);
            mTextview8 = itemView.findViewById(R.id.textview8);
            mTextview9 = itemView.findViewById(R.id.textview9);
            mTextview10 = itemView.findViewById(R.id.textview10);
            mTextview11 = itemView.findViewById(R.id.textview11);
            mTextview12 = itemView.findViewById(R.id.textview12);
            mIvIcon = itemView.findViewById(R.id.iv_icon);
            mCheckbox = itemView.findViewById(R.id.m_item_lid_checkbox);


        }

    }

    //两次点击按钮之间的间隔，目前为1000ms
    private static final int MIN_CLICK_DELAY_TIME = 1000;
    private long lastClickTime;

    public static interface OnItemClickListener {

        void onItemClick(View view, int position);

    }

    @Override
    public void onClick(View v) {
        if (mOnItemClickListener != null) {
            long curClickTime = System.currentTimeMillis();
            if ((curClickTime - lastClickTime) >= MIN_CLICK_DELAY_TIME) {
                lastClickTime = curClickTime;
                //注意这里使用getTag方法获取position
                mOnItemClickListener.onItemClick(v, (int) v.getTag());
            }
        }
    }

    public void setOnItemClickListener(IqcLldAdapter.OnItemClickListener listener) {
        this.mOnItemClickListener = listener;
    }
}


